package yangliu.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import yangliu.entity.BjEntity;
import yangliu.entity.Page;
import yangliu.entity.YangLiuEntity;
import java.util.List;
@Component()
public class YangLiuDao extends HibernateDaoSupport {
    public List<YangLiuEntity> queryPage(Page page, String name) {

        String sql = "select a.id,a.name,a.sex,a.address,b.bname from j05yangliu a,j05class b where a.bid=b.id and  name like ? order by a.id desc limit ?,? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        query.setParameter(1,  page.getBeginIndex());
        query.setParameter(2,  page.getEveryPage());
        query.setResultTransformer(Transformers.aliasToBean(YangLiuEntity.class));
        List<YangLiuEntity> list=  query.list();


        return  list;
    }

    public void save(YangLiuEntity yangliu) {
        Session session= getSessionFactory().openSession();
        session.saveOrUpdate(yangliu);
        session.flush();

    }

    public int queryCount(String name) {
        String sql = "select * from j05yangliu where name like ? ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setParameter(0,  "%" + name + "%");
        List<YangLiuEntity> list=  query.list();
        return  list.size();
    }

    public YangLiuEntity edit(YangLiuEntity yangliu) {
        return   (YangLiuEntity)getSessionFactory().openSession().get(YangLiuEntity.class,yangliu.getId());
    }

    public void del(YangLiuEntity yangliu) {
        Session session= getSessionFactory().openSession();
        session.delete(yangliu);
        session.flush();

    }

    public List<BjEntity> bj() {
        String sql = "select * from j05class  ";
        Query query=getSessionFactory().openSession().createSQLQuery(sql);
        query.setResultTransformer(Transformers.aliasToBean(BjEntity.class));
        List<BjEntity> list=  query.list();
        return  list;
    }
}
